#include "babel.h"
#include "ui_babel.h"
#include "client.h"

#include <QFileSystemModel>
#include <QDialog>
#include <QMenu>


Babel::Babel(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Babel)
{
    _client = new Client(this);

    ui->setupUi(this);

    ui->server->lineEdit()->setPlaceholderText("Server");
    ui->server->setAutoCompletion(true);
    ui->status->lineEdit()->setReadOnly(true);

    ui->message->hide();

    QFileSystemModel *model = new QFileSystemModel;
    model->setRootPath(QDir::currentPath());
    ui->contactTreeView->setModel(model);

    this->setMaximumSize(1, 1);
    ui->navigationWidget->setCurrentIndex(0);

    this->_client->connectToHost(ui->server->lineEdit()->text(), 4242);
    //TODO ANTOINE mettre un bouton pour se connecter au server (!authenticate) tant que la connexion echoue
    //si on est pas connecte desactiver le formulaire de connexion... (ou faire un timer ...etc)
    //this->_client.connectToHost("127.0.0.1", 4242);
}

Babel::~Babel()
{
    delete ui;
}

void Babel::on_connect_clicked()
{
    this->_client->authenticate();
    this->setMaximumSize(100000, 100000);

    //ui->navigationWidget->setCurrentIndex(1);
}

void Babel::on_disconnect_clicked()
{
    this->_client->logout();
}

void Babel::on_about_linkActivated(QString)
{
    ui->navigationWidget->setCurrentIndex(2);
}

void Babel::on_aboutOK_clicked()
{
    ui->navigationWidget->setCurrentIndex(0);
}

void Babel::on_cancel_clicked()
{
    this->close();
}

void Babel::on_server_textChanged(QString text)
{
    (void)text;
    this->_client->disconnectFromHost();
    this->_client->connectToHost(ui->server->lineEdit()->text(), 4242);
}

void Babel::on_pushButton_2_clicked()
{
    qDebug() << "I am" << ui->login->text() << ui->password->text();

    this->_client->reg();
}

void Babel::on_buttonaddcontact_clicked()
{
    this->_client->addContact();
}
